package fr.univ.lr.icone.log;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import fr.univ.lr.icone.agents.Agent;

public class Log implements ILog {

	private FileWriter writer;
	private Date currentDate;
	private String fileName;

	private static final String RETURN = "\r\n";
	private static final String LOG_HEAD_MESSAGE_BEGIN = "----------------------OPINION LOG (";
	private static final String LOG_HEAD_MESSAGE_END = ")----------------------"
			+ RETURN;

	public Log(String file) {
		this.fileName = file;
	}

	@Override
	public void log(List<Agent> agents) throws IOException {
		writer = new FileWriter(fileName, true);
		currentDate = new Date();
		writer.write(LOG_HEAD_MESSAGE_BEGIN + currentDate
				+ LOG_HEAD_MESSAGE_END);
		for (Agent a : agents) {
			writer.write("Agent " + a.getId() + " --> " + a.getOpinion()
					+ RETURN);
		}
		close();
	}

	public void close() throws IOException {
		writer.close();
	}
}
